﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    class UpdateSqlStaForMySQL : UpdateSqlStatement
    {       
        /// <summary>
        /// 构造函数
        /// </summary>
        public UpdateSqlStaForMySQL()
            : base() { }
       
        /// <summary>
        /// 转换成SQL
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            /******低代码改动:通过SqlBuildInfo中的SqlTable信息获取当前Statement的表及别名信息******/
            SqlTable sqlTable = this.SqlBuildingInfo.CurrentSqlTable;
            //注册用DB转义符包裹列名的方法
            var tableName = this.TableName;
            tableName = this.SqlBuildingInfo.WrapByCurrentDBEscapeCharacter(tableName);
            var tableAlias = sqlTable.TableAlias;
            tableAlias = this.SqlBuildingInfo.WrapByCurrentDBEscapeCharacter(tableAlias);
            this.UpdateFields.FieldNameWraps = this.SqlBuildingInfo.WrapByCurrentDBEscapeCharacter;

            StringBuilder builder = new StringBuilder();
            string updateSql = string.Format("UPDATE {0} {1} SET {2}",
                tableName, tableAlias, base.GetSetClause(this.UpdateFields, this.UpdateValues));
            builder.Append(updateSql);
            string whereCondition = this.GetUpdateConditionString();
            if (!IsBlankString(whereCondition))
                builder.AppendFormat(" WHERE {0}", whereCondition);

            return builder.ToString();
        }
    }
}
